/**
 * 
 */
package sudoku.ui;

import java.awt.Color;
import java.awt.Component;

import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;

import sudoku.controller.Controller;
import sudoku.model.SudokuModel;
import sudoku.util.Data;
import sudoku.util.Language;


/**
 * @author Romet
 *
 */
public class GameChoiceUI extends DefaultListCellRenderer{

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1L;

	private JList valik;
	
	private volatile Data data;
	private volatile SudokuUI m2ng;
	private volatile Controller controller;
	private volatile SudokuModel model;
	private volatile Language language;
	
	private int games;
	
	/**
	 * Vajalik uue DefaultListCellRenderer tegemiseks.
	 */
	public GameChoiceUI(){
		super();
	}

	/**
	 * Seadistab mängude valiku kasutajaliidese.
	 * @param data andmete klassi isend
	 * @param m2ng põhiakna graafika klassi isend
	 * @param controller kontrolleri klassi isend
	 * @param model põhiakna loogika klassi isend
	 */
	public GameChoiceUI(Language language, Data data, SudokuUI m2ng, Controller controller, SudokuModel model){
		this.language = language;
		this.data = data;
		this.m2ng = m2ng;
		this.controller = controller;
		this.model = model;
	}

	/* (non-Javadoc)
	 * @see javax.swing.ListCellRenderer#getListCellRendererComponent(javax.swing.JList, java.lang.Object, int, boolean, boolean)
	 */
	public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {		
		String tekst = value.toString();		
		if (tekst.startsWith("--")) {
			value = tekst.substring(2);
		}else{
			value = tekst.substring(2);
		}
		super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
		if (tekst.startsWith("--")){
			setForeground(Color.RED);
		}
		else{
			setForeground(new Color(0,200,0));
		}
		return this;
	}

	/**
	 * Tagastab mängude listi.
	 * @return mängude list
	 */
	public JList getValik() {
		return valik;
	}
	
	public void setGames(int games) {
		this.games = games;
	}

	public void setvalik() {
		DefaultListModel listModel = new DefaultListModel();
		for (int i=0; i<data.getOlemasolevadM2ngud(games).size(); i++){
			String label = "";
			String aeg = "";
			try{
				boolean tehtud = false;
				for (int j=0; j<data.getM2nguAjad().size(); j++){
						String[] s = ((String)data.getM2nguAjad().get(j)).split("\t");
				if (Integer.parseInt(s[0]) == i+1){
						tehtud = true;
						aeg = s[1];
						}
				}
				if (tehtud)
					label = "++";
				else{
					label = "--";
				}
			}catch(Exception er){
				label = "--";
			}
			if (i<9) {
				label += games+"x"+games+" "+language.getGameChoice_name()+String.valueOf(i+1)+"                "+aeg;
			} else if (i<99) {
				label += games+"x"+games+" "+language.getGameChoice_name()+String.valueOf(i+1)+"              "+aeg;
			} else{
				label += games+"x"+games+" "+language.getGameChoice_name()+String.valueOf(i+1)+"            "+aeg;
			}
			listModel.addElement(label);
		}
	    valik = new JList(listModel);
	    valik.setCellRenderer(new GameChoiceUI());
	    valiM2ng();
	}
	
	/**
	 * Teeb mängude valiku akna.
	 */
	public void valiM2ng(){
		valik.setVisibleRowCount(20);
        JScrollPane listScrollPane = new JScrollPane(valik);
		JOptionPane pane = new JOptionPane(listScrollPane);
		pane.setOptions(new Object[] {"OK", language.getButton_cancel()});;
		JDialog dialog = pane.createDialog(m2ng, language.getGameChoice_title());
		dialog.setVisible(true);
		Object selectedValue = pane.getValue();
		if (selectedValue != null && !selectedValue.equals(language.getButton_cancel())){
			if (valik.getSelectedIndex() != -1){
				model.setValitud_m2ng(valik.getSelectedIndex());
				controller.handleEvent(Controller.VALITUD_M2NG_EVENT);
			}
		}
	}
}
